package com.dream.xiaobo.controller;

import com.dream.xiaobo.entity.Type;
import com.dream.xiaobo.service.impl.TypeServiceImpl;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;

/**
 * @author xiaobo
 * @date 2021/8/31 - 10:24
 */
@WebServlet("/type")
public class TypeController extends BaseServlet {

    //创建typeService实例化对象
    private TypeServiceImpl typeService = new TypeServiceImpl();

    /**
     * 商品分类展示
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //调用typeService逻辑方法
        List<Type> types = typeService.findAll();

        //创建解决Json问题对象
        Gson gson = new Gson();
        //将信息转换为JSON格式
        String json = gson.toJson(types);

        //返回JSON数据
        return json;
    }

}
